// pages/users/users.js
import { storeBindingsBehavior, createStoreBindings } from 'mobx-miniprogram-bindings'
import { store } from '../../store/store'
const app= getApp();
import service from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:'',
    token:'',
    balance: 0.00,
    chargeorder:false,
    coupon: 0,
    devops: false,
    hasPile: false,
    isCompany:false,
    companyPrice: '',
    isGroup: false,
  },
  phoneCall(){
    wx.makePhoneCall({
      phoneNumber: '4001071368' 
    })
  },
  about(){
    wx.navigateTo({
      url: '/subpage/pages/platfrom/platfrom',
    })
  },
  repair(){
    wx.navigateTo({
      url: '/package1/pages/repair/index',
    })
  },
  userPile(){
    wx.navigateTo({
      url: '/package1/pages/pile/pile',
    })
  },
  personOrder(){
    wx.navigateTo({
      url: '/package1/pages/order/order',
    })
  },
  personCombo(){
    wx.navigateTo({
      url: '/package1/pages/combo/index',
    })
  },
  seeMember(){
    wx.showModal({
      title: '你已经是会员了',
      content: '限时活动：充电满一单即自动加入会员，享受会员价',
      success (res) {
        if (res.confirm) {
          console.log('用户点击确定')
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.storeBindings = createStoreBindings(this, {
      store,
      actions: ['updateActives'],
    })
   
    // this.getusers()
    console.log('useringo',this.data.userinfo)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  //是否有充电枪
  hasGun() {
    let that = this
    let res = service('GET', '/personal/gun/has').then(result => {
      console.log('充电枪',result)
      let data = result.data.hasPile
      
      that.setData({
        hasPile: data,
      })
    })
  },
  isdevops() {
    let that = this
    let res = service('GET', '/user/isdevops').then(result => {
      console.log('isdevops',result)
      
      that.setData({
        devops: result.data.devops,
      })
    })
  },
    //集团钱包
    getCompanyPrice() {
      let that = this
      let res = service('GET', '/group/user/wallet/info').then(result => {
        console.log('集团钱包',result)
        // let num = result.data.page.list.length
        that.setData({
          companyPrice: result.data.info,
        })
      })
    },
 //获取优惠券
 getCoupon(tye) {
  let that = this
  let res = service('POST', '/coupon/list', {
    state:1
  }).then(result => {
    console.log('优惠券',result)
    let num = result.data.page.list.length
    that.setData({
      coupon: num,
    })
  })
},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.updateActives(4)
    this.getusers()
    this.isdevops()
    if(!wx.getStorageSync('token')){
      this.selectComponent({
        token: ''
      })
      // wx.showModal({
      //   title: '请先授权登录',
      //   showCancel:true,
      //   confirmText:"去登陆",
      //   confirmColor:"#d0b307",
      //   success (res) {
      //     if (res.confirm) {
      //       wx.navigateTo({
      //         url: '/subpage/pages/login/login',
      //       })
      //     } else{ console.log('按钮错误')}
      //   }
      // })
      return
    }else{
      this.setData({
        token: wx.getStorageSync('token')
      })
    }
   

    this.hasGun()
    this.getCompanyPrice()
   this.getBalance()
   this.chargeOrder()
   this.getCoupon()
    console.log('监听显示')
    
  },
    //正在充电的订单
    chargeOrder() {
      let that = this
      let result = service('GET', '/order/charging').then(res => {
        console.log('正在充电订单', res)
        if (res.data.code == 0) {
          that.setData({
            chargeorder: true,
          })
        }
      })
    },
    //我的余额
    getBalance() {
      let that = this
      let res =  service("GET", '/wallet/money', {}).then(res => {
        // console.log('初始化金额', res)
        if (res.data.code == 0) {
          that.setData({
            balance: res.data.money.balance,
          })
        }
      })
    },
     //获取个人信息
     getusers(){
       let that = this
      let data = service('GET','/user/info',{}).then( res => {
        console.log('我获取了我的头像',res)
        if(res.data.code == 414){
          this.setData({
            token: '',
            userinfo: ''
          })
          wx.setStorageSync('token', '')
          wx.setStorageSync('userinfo', '')
          return
        }
        if( res.data.code ==0){
          console.log('res,res', res)
          that.setData({
            userinfo:res.data.user,
            token: wx.getStorageSync('token')
          })
          // res.data.phone.substring(3,6) + "******" 
          // if(res.data.name =='微信用户'){
            
          // }
          if(res.data.user.group){
            let group = res.data.user.group
            let time = new Date().getTime()
            if(group.state == 1 &&  (time >=group.effectTime && time<=group.invalidTime)){
              that.setData({
                isGroup: true
              })
            }
          }
          
        }else {
          wx.setStorageSync('userPhone',false)
          wx.setStorageSync('userinfo','')
        }
        
      })
    },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.storeBindings.destroyStoreBindings()
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  isLogin () {
    wx.navigateTo({
			url: "/subpage/pages/login/login"
		})
  },


  toInvitation () {
    console.log(this.data.userinfo)
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/invitation/invitation"
		})
  },
  //优惠券
  toCoupon () {
    console.log(this.data.userinfo)
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/coupon/coupon"
		})
  },
    //套餐
    toCombo () {
      console.log(this.data.userinfo)
      if (!this.data.token) {
        this.isLogin();
        return;
      }
      wx.navigateTo({
        url: "/subpage/pages/mycombo/mycombo"
      })
    },
  goInvoce(){
    if (!this.data.token) {
      this.isLogin();
      return;
    }
    wx.navigateTo({
      url: "/user/pages/invoce/invoce"
    })
  },
  toIntegral () {
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/integral/integral"
		})
  },
  mySurplus () {
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/balance/balance"
		})
  },
  toOrder (e) {
    console.log('eee',e)
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/order/order?state="+e.currentTarget.dataset.state
		})
  },
  toAppointe () {
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/appointe/appointe"
		})
  },
  toRecord() {
    if (!this.data.token) {
			this.isLogin();
			return;
		}
    wx.navigateTo({
			url: "/subpage/pages/record/record"
		})
  },
  toCard () {
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/card/card"
		})
  },
  toCollect (){
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/collection/collection"
		})
  },
  tocarBrand () {
    if (!this.data.token) {
			this.isLogin();
			return;
		}
		wx.navigateTo({
			url: "/subpage/pages/carbeand/carbeand"
		})
  },
  goLogin() {
    wx.navigateTo({
			url: "/subpage/pages/login/login"
		})
  },
  toeditPhone() {
    wx.navigateTo({
			url: "/subpage/pages/editphone/editphone"
		})
  },
  apply(){
    wx.navigateTo({
      url: '/package1/pages/apply/index',
    })
  },
  toDevops(){
    wx.navigateTo({
      url: '/devops/pages/order/order',
    })
  },
  logout (){
    wx.setStorageSync('userinfo', '')
    wx.setStorageSync('token', '')
    wx.setStorageSync('userPhone', '')
    this.setData({
      userinfo:'',
      token: ''
    })
    // wx.switchTab({
    //   url: '/pages/index/index',
    // })
  }
})